package com.fmi.timedealer.moodgame;

import java.util.ArrayList;

import com.fmi.timedealer.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.View;

public class BallView extends View {
	
	private Bitmap mBitmapObject;
	private float x;  					// Object's top left corner (x, y)
	private float y;
	private float xDegrees;
	private float yDegrees;
	private Paint paint;
	private Box mGameBox;
	private ArrayList<MoodView> moods;
	private OnCollisionListener collisionHandler;
	
	// Constructor
   	public BallView(Context context, DisplayMetrics metrics, Box playBox, ArrayList<MoodView> moods) {
   		super(context);
   		
   		mGameBox = playBox;
   		this.moods = moods;
   		
      	// To enable keypad
      	this.setFocusable(true);
      	this.requestFocus();
      	// To enable touch mode
      	this.setFocusableInTouchMode(true);
      	
      	this.paint = new Paint();
      	this.mBitmapObject = BitmapFactory.decodeResource(context.getResources(), R.drawable.ball);
      	
      	x = (mGameBox.getMaxX() - mGameBox.getMinX()) / 2 - (mBitmapObject.getWidth() / 2);
      	y = (mGameBox.getMaxY() - mGameBox.getMinY()) / 2 - (mBitmapObject.getHeight() / 2);
      	
      	xDegrees = 0;
      	yDegrees = 0;
      	collisionHandler = null;
   	}

	@Override
	protected void onDraw(Canvas canvas) {

		canvas.drawBitmap(mBitmapObject, x, y, paint);
		
		boolean hasCollision = false;
		for (MoodView mood : moods) {
			
			hasCollision = CircleUtils.hasCollision(this, mood);
			if (hasCollision) {
				if (collisionHandler != null) {
					collisionHandler.onCollision(mood);
				}
				break;
			}
		}
		
		if (!hasCollision) {
			x -= xDegrees;
			y -= yDegrees;
			
			if (x < mGameBox.getMinX()) {
				x = mGameBox.getMinX();
			}
			
			if (x > (mGameBox.getMaxX() - mBitmapObject.getWidth())) {
				x = mGameBox.getMaxX() - mBitmapObject.getWidth();
			}
			
			if (y < mGameBox.getMinY()) {
				y = mGameBox.getMinY();
			}
			
			if (y > (mGameBox.getMaxY() - mBitmapObject.getHeight())) {
				y = mGameBox.getMaxY() - mBitmapObject.getHeight();
			}
			
			this.invalidate();
		}
	}
	
	public void updateOrientation(float[] values) {
		xDegrees = values[1];
		yDegrees = -values[2];
	}
	
	public float getXCoordinate() {
		return this.x;
	}
	
	public float getYCoordinate() {
		return this.y;
	}
	
	public int getObjectWidth() {
		return this.mBitmapObject.getWidth();
	}
	
	public Point getCenter() {
		return CircleUtils.getCenter((int)x, (int)y, mBitmapObject);
	}
	
	public int getRadius() {
		return CircleUtils.getRadius(mBitmapObject);
	}
	
	public void setOnCollisionListener(OnCollisionListener listener) {
		collisionHandler = listener;
	}
	
	public interface OnCollisionListener {
		
		void onCollision(MoodView moodView);
	}
}
